<?php
class cart extends CI_Controller 
{
		
    function __construct()
	{
		parent::__construct();	
		$this->load->model('product_model');
		$this->load->model('category_model');
		$this->load->model('order_model');
		$this->load->model('order_product_model');
		//$this->load->language('cart','english');
	}
	function getPath($action = '')
	{
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index($page = 0)
	{
		$per_page_count 	     = $this->config->item('per_page_count');
		$view 				     = $this->getPath(__FUNCTION__);
		$search                  = $this->input->post('search');
		$where                    = array('deleted' => 0);
		if(!empty($search))
		{
			$name                  = $this->input->post('name');
			$view['filter2']        = $name ;
			$this->session->set_userdata(array('filter2' => $name));
			$code                  = $this->input->post('code');
			$view['filter3']        = $code ;
			$this->session->set_userdata(array('filter3' => $code));
			$parent                = $this->input->post('parent');
			$view['filter']        = $parent ;
			$this->session->set_userdata(array('filter' => $parent));
			$category              = $this->input->post('category');         // filter subject with cart 
			$view['filter1']       = $category ;  
			$this->session->set_userdata(array('filter1' => $category));    // set filter value
		}
		else
		{
			$view['filter']     = $this->session->userdata('filter');
			$view['filter1']     = $this->session->userdata('filter1');
			$view['filter2']     = $this->session->userdata('filter2');
			$view['filter3']     = $this->session->userdata('filter3');
		}
		if(!empty($view['filter1']))
		{
		$where['category']        = $view['filter1'];   //$cart;
		$sub                      = $this->category_model->getcategories(0,'','', array('deleted' => 0,'parent_id' => $view['filter']) );
		$view['sub']    		  = $sub->records;
		}
		if(!empty($view['filter2']))
		{
			$like['name']        = $view['filter2'];   //$cart;
		}
		if(!empty($view['filter3']))
		{
			$where['code']        = $view['filter3'];   //$cart;
		}
		
		$cart                  = $this->cart_model->getcarts($page, $per_page_count , $like, $where );
		$view['cart'] 		  = $cart->records;
		$view['cur_page'] 	      = $page;
		$view['title']		      = 'Standards Listing';
		$view['subtitle']	      = 'Standards Lists';
		/************************ Pagination block ***********************************/
		$config['base_url'] 	  = $this->config->item('admin_base_url').'/cart/index/';
		$config['total_rows'] 	  = $cart->total;
		$config['per_page']       = $per_page_count;
		$config['cur_page']       = $page /$per_page_count;
		$config['uri_segment'] 	  = 4;
		$config['num_links'] 	  = 3;
		$this->pagination->initialize($config);
		$view['msg'] 	          = $this->lang->line($this->session->flashdata('cart_msg'));
		
		$where                    = array('deleted' => 0,'parent_id' => '0');
		$category                 = $this->category_model->getcategories(0,'','', $where );
		$view['category'] 		  = $category->records;
		
		$this->layout->adminview('cart/cart_view', $view);
	
	}
	function add($id = '')
	{
		$mylogin = $this->session->userdata('mylogin');
		if(empty($mylogin))
		{
				redirect($this->config->item('base_url').'member/login/cart','location');
		}
		session_start();
		$order   = $this->input->post('order');
		if(!empty($order))
		{
			redirect($this->config->item('base_url').'cart/order/','location');
			
		}
		
		$update  = $this->input->post('update');
		if(!empty($update))
		{
			foreach($_SESSION['prod'] as $p=> $qty)
			{
				$_SESSION['prod'][$p]['qty'] = $_POST['qty'.$p];
			}
	
		}
	
		if(!empty($id))
			{	
				$pid                     = $id;
				if(isset($_SESSION['prod']))
					{
						//echo "yes";
					if(!in_array($id,$_SESSION['prod']))
						{
		
					     $_SESSION['prod'][$pid]  = array('pro'=>$id,'qty'=>1);
					    }
		             }
	            else
				{
					//echo "ff";
	            $_SESSION['prod'][$pid]=array('pro'=>$pid,'qty'=>1);
				}
             }
         // print_r($_SESSION['prod']);
		  foreach($_SESSION['prod'] as $p=>$v)
		  {
			//echo $p['pro']   ;
			$wherein[] = $v['pro'];
			      
		  }
		  //print_r($wherein);
		  
        $number_of_products       = count($_SESSION['prod']);	
		//echo $number_of_products ;
		$this->session->set_userdata('cart_count',  $number_of_products); 
		if(!empty($wherein))
		{
		$product                  = $this->product_model->getProducts($page, $per_page_count , '',array('deleted'=>0), $wherein );
		$view['product'] 		  = $product->records;
		}
		

		
		$this->layout->getView('header');
		$this->layout->getView('cart/cart_view',$view);
		$this->load->view('footer'); 
	}
	
	function remove_item($id='')
	{
		session_start();
		unset($_SESSION['prod'][$id]);
		redirect($this->config->item('base_url').'cart/add/'.$cur_page,'location');
	}
	function order()
	{
		session_start();
		$send = $this->input->post('send');
		if(!empty($send))
		{
			redirect($this->config->item('base_url').'cart/ordered_items/'.$cur_page,'location');
		}
		
		foreach($_SESSION['prod'] as $p=>$v)
		  {
			//echo $p['pro']   ;
			$wherein[] = $v['pro'];
			      
		  }
		  
		$number_of_products       = count($_SESSION['prod']);	
		$this->session->set_userdata('cart_count',  $number_of_products); 
		$product                  = $this->product_model->getProducts($page, $per_page_count , '',array('deleted'=>0), $wherein );
		$view['product'] 		  = $product->records;
		
		$this->layout->getView('header');
		$this->layout->getView('cart/order_view',$view);
		$this->load->view('footer');
	}
    function ordered_items()
	{
		session_start();
		$send = $this->input->post('send');
		if(!empty($send))
		{
			redirect($this->config->item('base_url').'cart/ordered_items/'.$cur_page,'location');
		}
		
		foreach($_SESSION['prod'] as $p=>$v)
		{
			//echo $p['pro']   ;
			$wherein[] = $v['pro'];
			//$total     = 5; 
			      
		  }
		  
		$number_of_products       = count($_SESSION['prod']);	
		$this->session->set_userdata('cart_count',  $number_of_products); 
		$product                  = $this->product_model->getProducts($page, $per_page_count , '',array('deleted'=>0), $wherein );
		$view['product'] 		  = $product->records;
		
		$data                     = array();

		 //print_r($mylogin);
	
		
		$this->layout->getView('header');
		$this->layout->getView('cart/ordered_items',$view);
		$this->load->view('footer');
		
    }
	
	function view($id = '',$cur_page='')
	{
		
		$cart          = $this->cart_model->getcarts(0,1,$like, array('id' => $id));
		
		$view['cart'] = $cart->records[0];
		
		
		$this->load->view('admin/cart/cart_info', $view);
	}
	
	function cancel()
	{
    redirect($this->config->item('admin_base_url').'cart/index','location');
		exit;	
	}
	
	function delete($id = '', $cur_page)
	{
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url').'cart/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$deleteStatus = $this->cart_model->delete($id);
			switch($deleteStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'cart_delete_success');
					redirect($this->config->item('admin_base_url').'cart/index/'.$cur_page,'location');
					exit;		
					break;
				case false:
					$view['msg'] 	    = $this->lang->line('cart_delete_failure');
					break;
			}
		}
		
	}
	function clearFilter()
	{
		$this->session->unset_userdata('filter');
		$this->session->unset_userdata('filter1');
		$this->session->unset_userdata('filter2');
		$this->session->unset_userdata('filter3');
		redirect($this->config->item('admin_base_url').'cart/index/','location');
	}
}
?>